export function awaitWrapper<T, U extends any>(fn: Promise<T>) {
  return fn
    .then<[null, T]>((data: T) => [null, data])
    .catch<[U, null]>((err) => [err, null]);
}
const DIVIDED_NUMBER = 5;
export function getBezierPoint(coords1: number[], coords2: number[]) {
  // y=-(x2-x1/(y2-y1)* [x-(x1+x2)/2]+(y1+y2)/2
  // 45deg line
  let [x1, y1] = coords1;
  let [x2, y2] = coords2;
  let [xm, ym] = [(x1 + x2) / 2, (y1 + y2) / 2];

  if (x1 === x2) return [x1 + Math.abs((y2 - y1) / DIVIDED_NUMBER), ym];
  if (y1 === y2) return [xm, y1 + Math.abs((x2 - x1) / DIVIDED_NUMBER)];
  let k = (y2 - y1) / (x2 - x1),
    b = y1 - k * x1,
    kc = -1 / k,
    bc = ym - kc * xm,
    d = Math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) / DIVIDED_NUMBER;
  d = (y2 > 180 ? -1 : 1) * d;
  let xn = (bc - b + d * Math.sqrt(Math.pow(k, 2) + 1)) / (k - kc),
    yn = kc * xn + bc;
  return [xn, yn];
}

export function addPrefixZero(num: number) {
  if (num < 10) return '0' + num;
  else return num;
}
